package com.glsc.ngateway.platform.controller.gitee;

import cn.hutool.core.date.DateUtil;
import com.glsc.ngateway.common.api.common.dto.PageQueryParamDto;
import com.glsc.ngateway.common.api.platform.dto.gitee.*;
import com.glsc.ngateway.common.api.platform.dto.gitee.issueBase.IssueFileDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.gitee.IssueBase;
import com.glsc.ngateway.platform.service.gitee.GiteeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

/**
 * @author zhengyr
 * @date 2024/1/26-18:55
 */
@RestController
@RequestMapping("/gitee")
@Api(value = "gitee", tags = {"gitee"})
public class GiteeController {

    @Resource
    GiteeService giteeService;

    @RequestMapping(method = RequestMethod.POST, value = "issues/groupList")
    @ApiOperation(value = "查询任务按人员分组", notes = "查询任务按人员分组")
    public PlatformResponse<List<IssueOutVo>> getIssuesGroupList(@RequestBody SearchIssueParamDto params) {
        return PlatformResponse.successData(giteeService.getGroupMapList(params));
    }

    @RequestMapping(method = RequestMethod.GET, value = "issues/exportWeekList")
    @ApiOperation(value = "周报导出", notes = "周报导出")
    public ResponseEntity<byte[]> export(
            @ApiParam(value = "负责人id") @RequestParam("assigneeId") String assigneeId,
            @ApiParam(value = "负责人中文名") @RequestParam("assigneeName") String assigneeName,
            @ApiParam(value = "计划结束日yyyy-mm-dd" ,required=false)  @RequestParam(value = "deadline",required = false) String deadline
    ) throws IOException {
        return giteeService.exportGroupList(assigneeId, assigneeName, deadline);

    }
//    @RequestMapping(method = RequestMethod.POST, value = "issues/listPage")
//    @ApiOperation(value = "查询任务列表分页", notes = "查询任务列表分页")
//    public PlatformResponse<Page<IssueDto>> getIssues(@RequestBody SearchIssueParamDto params){
//        return PlatformResponse.successData(giteeService.queryIssues(params)) ;
//    }

    @RequestMapping(method = RequestMethod.POST, value = "members/listPage")
    @ApiOperation(value = "查询gitee成员分页", notes = "查询gitee成员分页")
    public PlatformResponse<Page<GiteeMemberDto>> getMembers(@RequestBody SearchGiteeMemberParamDto params) {
        return PlatformResponse.successData(giteeService.queryMembers(params));
    }

    @RequestMapping(method = RequestMethod.GET, value = "issues/findByIdent")
    @ApiOperation(value = "根据ident查询任务明细", notes = "根据ident查询任务明细")
    public PlatformResponse<IssueDetailDto> getIssueByIdent(@RequestParam("ident") String ident) {
        return PlatformResponse.successData(giteeService.queryIssueByIdent(ident));
    }

    @RequestMapping(method = RequestMethod.GET, value = "issues/getFileList")
    @ApiOperation(value = "根据issue id查询附件列表", notes = "根据issue id查询附件列表")
    public PlatformResponse<List<IssueFileDto>> getFileListById(@RequestParam("id") String id) {
        return PlatformResponse.successData(giteeService.queryIssueFileList(id));
    }

    @RequestMapping(method = RequestMethod.GET, value = "issues/downloadFileById")
    @ApiOperation(value = "根据附件id下载附件", notes = "根据附件id下载附件")
    public ResponseEntity<byte[]> downloadFileById(@RequestParam("fileId") String fileId) {
        return giteeService.downLoadIssueFileById(fileId);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/issues/syncAllIssuee")
    @ApiOperation(value = "同步任务至本地", notes = "同步任务至本地")
    public PlatformResponse<?> syncAllIssuee(@RequestParam(value = "date", required = false)
                                             @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
        giteeService.syncAllIssue(date);
        return PlatformResponse.success();
    }

    @RequestMapping(method = RequestMethod.GET, value = "/issues/page")
    @ApiOperation(value = "同步任务至本地", notes = "同步任务至本地")
    public PlatformResponse<Page<IssueBase>> page(PageQueryParamDto param) {
        return PlatformResponse.successData(giteeService.pageIssueBase(param));
    }
}
